package com.fsxgt.datagrid.console.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fsxgt.datagrid.console.bo.JobInfoBO;
import com.fsxgt.datagrid.console.exception.RunningException;
import com.fsxgt.datagrid.console.page.PageParam;
import com.fsxgt.datagrid.console.service.impl.JobService;
import com.fsxgt.datagrid.core.exception.DataGridException;
import com.fsxgt.datagrid.core.utils.ReturnT;
import com.fsxgt.datagrid.flow.service.impl.FlowMenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "任务接口", value = "JobController")
@RequestMapping(produces = "application/json; charset=UTF-8",path = "/job")
@RestController
public class JobController extends BaseController {

	@Autowired
	private JobService jobService;
	@Autowired
	private FlowMenuService flowMenuService;
	
	

	@ApiOperation(hidden = true, value = "新增任务", notes = "")
	@RequestMapping(path = "/addJob", method = RequestMethod.POST)
	public ReturnT addJob(@RequestBody JobInfoBO jobInfoBO, HttpServletRequest request) throws DataGridException {


		jobService.addJob(jobInfoBO);

		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "修改任务", notes = "")
	@RequestMapping(path = "/modifyJob", method = RequestMethod.POST)
	public ReturnT modifyJob(@RequestBody JobInfoBO jobInfoBO, HttpServletRequest request) throws DataGridException {


		jobService.modifyJob(jobInfoBO);

		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "获取任务", notes = "")
	@RequestMapping(path = "/getJob", method = RequestMethod.POST)
	public ReturnT getJob(@RequestBody JobInfoBO jobInfoBO, HttpServletRequest request) throws DataGridException {

		return new ReturnT(jobService.getJob(jobInfoBO));

	}
	
	@ApiOperation(hidden = true, value = "删除任务", notes = "")
	@RequestMapping(path = "/deleteJob", method = RequestMethod.POST)
	public ReturnT deleteJob(@RequestBody JobInfoBO jobInfoBO, HttpServletRequest request) throws DataGridException {


		jobService.deleteJob(jobInfoBO.getId());

		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "打开任务", notes = "")
	@RequestMapping(path = "/openJob", method = RequestMethod.POST)
	public ReturnT openJob(@RequestBody JobInfoBO jobInfoBO, HttpServletRequest request) throws DataGridException {


		jobService.openJob(jobInfoBO.getId());

		return new ReturnT();

	}
	
	
	
	@ApiOperation(hidden = true, value = "关闭任务", notes = "")
	@RequestMapping(path = "/closeJob", method = RequestMethod.POST)
	public ReturnT closeJob(@RequestBody JobInfoBO jobInfoBO, HttpServletRequest request) throws DataGridException {


		jobService.closeJob(jobInfoBO.getId());

		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "执行任务", notes = "")
	@RequestMapping(path = "/startJob", method = RequestMethod.POST)
	public ReturnT startJob(@RequestBody JobInfoBO jobInfoBO, HttpServletRequest request) throws  RunningException {



		jobService.execJob(jobInfoBO.getId());

		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "下次执行时间", notes = "")
	@RequestMapping(path = "/getNextTime", method = RequestMethod.POST)
	public ReturnT nextTime(@RequestBody JobInfoBO jobInfoBO, HttpServletRequest request)  {
		return new ReturnT(jobService.getNextTime(jobInfoBO.getId()));
	}
	
	@ApiOperation(hidden = true, value = "获取流程字典", notes = "")
	@RequestMapping(path = "/getFlowInfo", method = RequestMethod.POST)
	public ReturnT getFlowInfo(HttpServletRequest request)  {
		return new ReturnT(jobService.getFlowInfo());
	}
	@ApiOperation(hidden = true, value = "获取流程字典", notes = "")
	@RequestMapping(path = "/getDataWork", method = RequestMethod.POST)
	public ReturnT getDataWork(HttpServletRequest request)  {
		return new ReturnT(jobService.getDataWork());
	}
	
	@ApiOperation(hidden = true, value = "分页查询", notes = "")
	@RequestMapping(path = "/findJobPage", method = RequestMethod.POST)
    public ReturnT findJobPage(@RequestBody PageParam<JobInfoBO> pageParam) {
		
		PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
		
		List<JobInfoBO> list=jobService.findJobInfoPage(pageParam.getParam());
		
		PageInfo<JobInfoBO> pageInfo = new PageInfo<>(list);
		
		
        return new ReturnT(pageInfo);
    }
    
	@ApiOperation(hidden = true, value = "获取所有job", notes = "")
	@RequestMapping(path = "/findJobList", method = RequestMethod.POST)
    public ReturnT findJobList(@RequestBody JobInfoBO jobInfoBO) {
		List<JobInfoBO> list=jobService.findSelectJobList(null);
        return new ReturnT(list);
    }
	
	@ApiOperation(hidden = true, value = "子任务下拉框", notes = "")
	@RequestMapping(path = "/findSelectJobList", method = RequestMethod.POST)
    public ReturnT findSelectJobList(@RequestBody JobInfoBO jobInfoBO) {
		List<JobInfoBO> list=jobService.findSelectJobList(jobInfoBO.getId());
        return new ReturnT(list);
    }
	

}